#include<bits/stdc++.h>
using namespace std;

int f[1000005];

int n, m;

void init() {
    for(int i = 1; i <= n * m; i++)
        f[i] = i;
}

int find(int x) {
    if(f[x] != x)	f[x] = find(f[x]);
    return f[x];
}

void union_set(int x, int y) {
    x = find(x);
    y = find(y);
    if(x != y)	f[x] = f[y];
}

signed main(){
//	system("start https://www.lanqiao.cn/problems/110/learning/");

	int k;
	cin >> n >> m >> k;
	
	init();
	
	for(int i = 1; i <= k; i++){
		int a, b;
		cin >> a >> b;
		union_set(a, b);
	}
	
	set<int> st;
	for(int i = 1; i <= n * m; i++)
		st.insert(find(i));
		
	cout << st.size();
	
	return 0;
}

